<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:c="http://java.sun.com/jsp/jstl/core">
	<ui:composition template="/templates/padraoJSF.xhtml">
		<ui:define name="barraTitulo">Cadastro de Grade Curricular</ui:define>
		<ui:define name="conteudo">
			<!-- MODAL DISCIPLINA -->
			<p:dialog header="Pesquisas Disciplina" width="640" widgetVar="modalPsqDisciplina" modal="true" onCloseUpdate="form" fixedCenter="true" effect="FADE">
				<h:form id="formDisciplinaPesquisa">
					<p:tabView id="tabView">
						<p:tab title="Pesquisa de Disciplina">
							<fieldset>
								<h:panelGrid columns="2" columnClasses="alinhaEsq width50">
									<h:column>
										<h:outputText value="Descri&ccedil;&atilde;o:" styleClass="textfield" />
									</h:column>
									<h:column>
										<h:inputText value="#{controleGradeCurricular.pesquisaDisciplinaDescricao}"  size="80" styleClass="textfield" />
									</h:column>
								</h:panelGrid>
								<h:panelGrid columns="2">
									<p:commandLink action="#{controleGradeCurricular.pesquisaDisciplina}" update=":listaDisciplinaPesquisa">
										<h:graphicImage url="/images/botoes/btnPesquisar.png" class="btnAcoes"/>
									</p:commandLink>
								</h:panelGrid>
							</fieldset>
						</p:tab>
					</p:tabView>
				</h:form>
				<h:form id="listaDisciplinaPesquisa">
					<p:dataTable value="#{controleGradeCurricular.listaDisciplinas}" 
					             id="id_ListaDisciplina"
					             var="disciplina" 
					             paginator="true" 
					             rows="10" 
					             dynamic="true" 
					             rendered="#{not empty controleGradeCurricular.listaDisciplinas}" 
								 firstPageLinkLabel="Primeiro"
					             previousPageLinkLabel="Anterior"
					             nextPageLinkLabel ="Pr&oacute;ximo"
					             lastPageLinkLabel="&Uacute;ltima"
					             emptyMessage="Nenhum resgistro encontrado"
					             errorMessage="Ocorreu um erro"
					             selectionMode="single" >
						<p:column styleClass="width60"  >
							<f:facet name="header">
								<h:outputText value="C&oacute;digo" />
							</f:facet>
							<h:outputText value="#{disciplina.codigoDisciplina}" />
						</p:column>
						<p:column>
							<f:facet name="header">
								<h:outputText value="Descri&ccedil;&atilde;o" />
							</f:facet>
							<h:outputText value="#{disciplina.descricao}" />
						</p:column>
						<p:column resizable="true" styleClass="width80">
							<f:facet name="header">
								<h:outputText value="A&ccedil;&atilde;o" /> 
							</f:facet>
                                 <h:commandLink action="#{controleGradeCurricular.addGradeDisciplina}">  
                                    <p:graphicImage url="/images/marcadores/editar.png" alt="Seleciona Aluno"/>  
                                    <f:setPropertyActionListener target="#{controleGradeCurricular.disciplina}" value="#{disciplina}" />
                                 </h:commandLink>
                     	</p:column>
					</p:dataTable>
				</h:form>
			</p:dialog>
			<!-- MODAL PRE-REQUISITO -->
			<p:dialog header="Pesquisas Pr&eacute;-requisito" width="640" widgetVar="modalPsqPreRequisito" modal="true" onCloseUpdate="form" fixedCenter="true" effect="FADE">
				<h:form id="formPreRequisitoPesquisa">
					<div id="pesquisarRolagem">
						<p>&nbsp;</p>
						<h2 class="tituloPesquisa">
							<h:outputLabel value="#{controleGradeCurricular.selecionadoGradeDisciplina.disciplina.descricao} (#{controleGradeCurricular.selecionadoGradeDisciplina.disciplina.codigoDisciplina})"/>
						</h2>
						<c:choose>
							<c:when test="#{controleGradeCurricular.gradeCurricular.gradeDisciplinas != null}">
								<c:set var="controlePeriodo" value="0"/>
								<ui:repeat var="gradeDisciplina" value="#{controleGradeCurricular.gradeCurricular.gradeDisciplinas}">
									<c:if test="#{controlePeriodo != gradeDisciplina.periodo}">
										<c:set var="controlePeriodo" value="0"></c:set>
									</c:if>
									<h:panelGrid columns="1" rendered="#{controleGradeCurricular.selecionadoGradeDisciplina.periodo > gradeDisciplina.periodo}">
										<h:column>
											<div class="resultadoPesquisa">
												<div class="resultadoPesquisa">
													<h5><h:outputLabel value="#{gradeDisciplina.periodo}"/>&ordm; Per&iacute;odo</h5>
													<table>
														<tr class="titulo">
															<td class="width100">Identificador</td>
															<td>Disciplina</td>
															<td class="width20 alinhaMeio">Selecione</td>
														</tr>
														<tr class="corUm">
															<td class="width100"><h:outputLabel value="#{gradeDisciplina.disciplina.identificador}"/></td>
															<td><h:outputLabel value="#{gradeDisciplina.disciplina.descricao}"/></td>
															<td class="width20 alinhaMeio">
								                                 <h:commandLink action="#{controleGradeCurricular.addPreRequisito}">  
								                                    <p:graphicImage url="/images/marcadores/tick.png" alt="Seleciona Aluno"/>  
								                                    <f:setPropertyActionListener target="#{controleGradeCurricular.preRequisitoGradeDisciplina}" value="#{gradeDisciplina}" />
								                                 </h:commandLink>
															</td>
														</tr>
													</table>
												</div>
											</div>
										</h:column>
									</h:panelGrid>
								</ui:repeat>
							</c:when>
						</c:choose>
					</div>
				</h:form>
			</p:dialog>
			<!-- MODAL EXCLUIR DISCIPLINA -->
			<p:dialog widgetVar="modalExclusaoDisciplina" header="Mensagem" fixedCenter="true" visible="false">
				<h:form >
					<h:panelGrid columns="1">
						<h:outputText value="Deseja Realmente Excluir?"/>
						<h:column >
			            	<p:commandButton value="Sim" action="#{controleGradeCurricular.excluirGradeDisciplina}" update=":form" oncomplete="modalExclusaoDisciplina.hide();"/>  
			                <p:commandButton value="N&atilde;o" update="form" oncomplete="modalExclusaoDisciplina.hide();"/>
						</h:column>
					</h:panelGrid>
				</h:form>
			</p:dialog>
			<!-- MODAL EXCLUIR PRE REQUISITO -->
			<p:dialog widgetVar="modalExclusaoPreRequisito" header="Mensagem" fixedCenter="true" visible="false">
				<h:form >
					<h:panelGrid columns="1">
						<h:outputText value="Deseja Realmente Excluir?"/>
						<h:column >
			            	<p:commandButton value="Sim" action="#{controleGradeCurricular.excluirPreRequisito}" update=":form" oncomplete="modalExclusaoPreRequisito.hide();"/>  
			                <p:commandButton value="N&atilde;o" update="form" oncomplete="modalExclusaoPreRequisito.hide();"/>
						</h:column>
					</h:panelGrid>
				</h:form>
			</p:dialog>
			<h:form id="form">
				<p:growl id="messages"/>
				<fieldset>
					<p:tabView>
						<p:tab title="Cadastro">
							<h:panelGrid columns="1">
								<h:column>
									<h:panelGrid columns="2" columnClasses="width150 alinhaDir" >
										<h:column >
										</h:column>
										<h:column >
											<h:outputText value="Os campos com asterisco (*) s&atilde;o de preenchimento obrigat&oacute;rio." styleClass="campoObrigatorio" />
										</h:column>
									</h:panelGrid>
								</h:column>
								<h:column>
									<h:panelGrid columns="3" columnClasses="width150 alinhaDir, width10" >
										<h:outputLabel value="Descri&ccedil;&atilde;o:" />
										<h:inputText value="#{controleGradeCurricular.gradeCurricular.descricao}" styleClass="textfield" size="100" />
										<h:outputText value="(*)" styleClass="campoObrigatorio" />
									</h:panelGrid>
								</h:column>
								<h:column>
									<h:panelGrid columns="3" columnClasses="width200,  , width200" >
										<h:column />									
										<p:pickList value="#{controleGradeCurricular.listaCursos}" 
											var="curso" 
											itemLabel="#{curso.descricao}" 
											itemValue="#{curso}" 
											converter="converteCurso"
											iconOnly="true" effect="bounce">
											<f:facet name="sourceCaption">Todos os Cursos</f:facet>  
											<f:facet name="targetCaption">Cursos Selecionados</f:facet> 
										</p:pickList>
										<h:column />
									</h:panelGrid>
								</h:column>
								<h:column>
									<h:panelGrid columns="12" columnClasses="width150 alinhaDir, width10, width20, width50, width50, width20, width30, width10, width140, width10, width10" >
										<h:outputLabel value="Ano:" />
										<h:inputText value="#{controleGradeCurricular.gradeCurricular.ano}" styleClass="textfield" size="4" maxlength="4"/>
										<h:outputText value="(*)" styleClass="campoObrigatorio" />
										<h:outputLabel value="Semestre:" />
										<h:selectOneMenu value="#{controleGradeCurricular.gradeCurricular.semestre}" id="semestre">
											<f:selectItem itemLabel="selecione" itemValue="0"/>
						                    <f:selectItem itemLabel="1" itemValue="1"/>
						                    <f:selectItem itemLabel="2" itemValue="2"/>
										</h:selectOneMenu>
										<h:outputText value="(*)" styleClass="campoObrigatorio" />
										<h:outputLabel value="Ativa:"/>
										<h:selectBooleanCheckbox value="#{controleGradeCurricular.gradeCurricular.ativo}"/>
										<h:outputLabel value="Quantidade de Per&iacute;odos:"/>
										<h:selectOneMenu value="#{controleGradeCurricular.gradeCurricular.periodos}" id="periodos">
											<f:selectItems value="#{controleGradeCurricular.listaPeriodos}" var="periodo" itemLabel="#{periodo}" itemValue="#{periodo}"/>
										</h:selectOneMenu>
										<h:outputText value="(*)" styleClass="campoObrigatorio" />
										<h:column >
											<p:commandLink action="#{controleGradeCurricular.atualizaPeriodos}" update=":form">
												<h:graphicImage url="/images/botoes/btnGerar.png" class="btnAcoes" />
											</p:commandLink>
										</h:column>
									</h:panelGrid>
								</h:column>
							</h:panelGrid>
						</p:tab>
					</p:tabView>
				</fieldset>
				<fieldset>
					<h:panelGrid columns="1" columnClasses="alinhaEsq" id="id_gradeDisciplinas">
						<h:column>
							<h:panelGrid rendered="#{not empty controleGradeCurricular.gradeCurricular.periodos and controleGradeCurricular.gradeCurricular.periodos != 0}">
								<p:accordionPanel activeIndex="#{controleGradeCurricular.accordionIndex}" multiple="true">
									<c:forEach var="periodo" begin="1" end="#{controleGradeCurricular.gradeCurricular.periodos}" varStatus="index">
										<p:tab title="#{periodo}&deg; Per&iacute;odo">
											<p:commandLink oncomplete="modalPsqDisciplina.show();">
												<f:setPropertyActionListener target="#{controleGradeCurricular.periodoSelecionado}" value="#{periodo}" />
												<f:setPropertyActionListener target="#{controleGradeCurricular.accordionIndex}" value="#{index.count}" />
												<h:graphicImage url="/images/botoes/btnAdicionarDisciplina.png" alt="Alterar"/>
											</p:commandLink>
											<div class="disciplinas">
												<table>
													<tr class="titulo">
														<td class="width100 alinhaMeio"><strong>Identificador</strong></td>
														<td><strong>Disciplina</strong></td>
														<td class="alinhaMeio"><strong>Cr&eacute;dito</strong></td>
														<td class="alinhaMeio"><strong>Carga Hor&aacute;ria</strong></td>
														<c:if test="#{periodo != 1}">
															<td class="alinhaMeio">
																<strong>Pr&eacute;-requisito</strong>
															</td>
														</c:if>
														<td class="alinhaMeio">
															<strong>A&ccedil;&otilde;es</strong>
														</td>
													</tr>
													<c:set var="corTabela" value="corDois"/>
													<c:forEach var="gradeDisciplina" items="#{controleGradeCurricular.gradeCurricular.gradeDisciplinas}">
														<c:choose>
															<c:when test="#{gradeDisciplina.periodo==periodo}">
																<c:if test="#{corTabela != 'corUm'}" var="controleCor">
																		<c:set var="corTabela" value="corUm"/>
																</c:if>
																<c:if test="#{corTabela != 'corDois' and controleCor == false}">
																	<c:set var="corTabela" value="corDois"/>
																</c:if>
																<tr class="#{corTabela}">
																	<td class="alinhaMeio"><strong><h:outputLabel value="#{gradeDisciplina.disciplina.identificador}"/></strong></td>
																	<td><strong><h:outputLabel value="#{gradeDisciplina.disciplina.descricao}"/></strong></td>
																	<td class="alinhaMeio"><strong><h:outputLabel value="#{gradeDisciplina.disciplina.creditos}"/></strong></td>
																	<td class="alinhaMeio"><strong><h:outputLabel value="#{gradeDisciplina.disciplina.cargaHoraria}"/></strong></td>
																	<c:if test="#{gradeDisciplina.periodo != 1}">
																		<td class="alinhaMeio">
																			<p:commandLink onclick="modalPsqPreRequisito.show();" update=":form :formPreRequisitoPesquisa">
																				<f:setPropertyActionListener target="#{controleGradeCurricular.selecionadoGradeDisciplina}" value="#{gradeDisciplina}" />
																				<f:setPropertyActionListener target="#{controleGradeCurricular.accordionIndex}" value="#{index.count}" />
																				<h:graphicImage value="/images/marcadores/add.png"/>
																			</p:commandLink>
																		</td>
																	</c:if>
																	<td class="alinhaMeio">
																		<p:commandLink onclick="modalExclusaoDisciplina.show();">  
																			<p:graphicImage url="/images/marcadores/excluir.png" alt="Excluir"/>  
																			<f:setPropertyActionListener target="#{controleGradeCurricular.selecionadoGradeDisciplina}" value="#{gradeDisciplina}" />
																			<f:setPropertyActionListener target="#{controleGradeCurricular.accordionIndex}" value="#{index.count}" />
																		</p:commandLink>
																	</td>
																</tr>
																<!-- Mostra o(s) pre-requisito(s) -->
																<c:if test="#{gradeDisciplina.periodo != 1}">
																	<tr>
																		<td>
																		</td>
																		<td colspan="5">
																			<c:if test="#{not empty gradeDisciplina.preRequisitos}">
																				<div>
																					<div class="btnComplemento">
																						Pr&eacute;-requisito
																					</div>
																					<table>
																						<tr class="titulo">
																							<td class="alinhaMeio">Identificador</td>
																							<td>Disciplina</td>
																							<td class="alinhaMeio">Per&iacute;odo</td>
																							<td class="alinhaMeio">Remover</td>
																						</tr>
																						<ui:repeat var="preRequisito" value="#{gradeDisciplina.preRequisitos}">
																							<tr>
																								<td class="alinhaMeio"><h:outputLabel value="#{preRequisito.disciplina.identificador}"/></td>
																								<td><h:outputLabel value="#{preRequisito.disciplina.descricao}"/></td>
																								<td class="alinhaMeio"><h:outputLabel value="#{preRequisito.periodo}"/></td>
																								<td class="alinhaMeio">
																									<p:commandLink onclick="modalExclusaoPreRequisito.show();">  
																										<h:graphicImage value="/images/marcadores/remove.png"/>
																										<f:setPropertyActionListener target="#{controleGradeCurricular.selecionadoGradeDisciplina}" value="#{gradeDisciplina}" />
																										<f:setPropertyActionListener target="#{controleGradeCurricular.preRequisitoGradeDisciplina}" value="#{preRequisito}" />
																										<f:setPropertyActionListener target="#{controleGradeCurricular.accordionIndex}" value="#{index.count}" />
																									</p:commandLink>
																								</td>
																							</tr>
																						</ui:repeat>
																					</table> 
																				</div>
																			</c:if>
																		</td>
																	</tr>
																</c:if>
															</c:when>
														</c:choose>
													</c:forEach>
												</table>
											</div>
										</p:tab>
									</c:forEach>
								</p:accordionPanel>
							</h:panelGrid>
						</h:column>
					</h:panelGrid>
				</fieldset>
				<h:outputLink value="manutencao.jsf">
					<h:graphicImage url="/images/botoes/btnPesquisar.png" class="btnAcoes" />
				</h:outputLink>
				<h:commandLink action="#{controleGradeCurricular.gravar}" styleClass="alinhaDir">
					<h:graphicImage url="/images/botoes/btnGravar.png" class="btnAcoes"/>
				</h:commandLink>
			</h:form>
		</ui:define>
	</ui:composition>
</html>